package com.demo;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class MapDemo1 {
    public static void main(String[] args) {

        /**
         * 创建Map集合对象 , 往集合中添加以下三对元素 , 使用获取Entry对象集合,在找到键和值 遍历集合
         *
         * 张无忌  --  赵敏
         * 张翠山  --  殷素素
         * 张三丰  --  郭芙
         */
        Map<Student, String> treeMap = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return -(o1.getAge() - o2.getAge());
            }
        });
        Student stu1 = new Student("lee", 18);
        Student stu2 = new Student("jack", 16);
        Student stu3 = new Student("lee", 18);
        Student stu4 = new Student("jack", 16);
        treeMap.put(stu1, "广东");
        treeMap.put(stu2, "上海");
        treeMap.put(stu3, "深圳");
        treeMap.put(stu4, "佛山");

        Set<Map.Entry<Student, String>> entries = treeMap.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key.getName() + "--" + key.getAge() + " -> " + value);
            // lee--18 -> 深圳
            // jack--16 -> 佛山
        }


    }
}
